﻿using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.ViewModels;
using System.Collections.Generic;
using Fayshine.VA.RIAService.Web;
using System.Windows.Controls.Theming;
using System.Windows;
using Fayshine.VA.Helpers;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class ShopSettings : Page
    {
        private ShopSettingViewModel m_ViewModel = null;
        private bool m_IsConfigUser = true;
        public ShopSettings()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ShopSettings_Loaded);
        }

        void ShopSettings_Loaded(object sender, RoutedEventArgs e)
        {
            ValidUserRole();
            m_ViewModel = new ShopSettingViewModel();
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllAreas();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAddShop_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtShop.Text))
            {
                MessageBox.Show("店铺名称不能为空！");
                return;
            }
            if (!m_ViewModel.ExistShop(txtShop.Text))
            {
                area shop = new RIAService.Web.area();
                shop.Name = txtShop.Text;
                shop.AreaTypeID = 1;
                m_ViewModel.AddArea(shop);
            }
            else
            {
                MessageBox.Show("店铺 " + txtShop.Text + " 已经存在！");
            }
        }

        private void btnUpdateShop_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtShop.Text))
            {
                MessageBox.Show("店铺名称不能为空！");
                return;
            }
            area selectShop = listShops.SelectedItem as area;
            if (selectShop != null)
            {
                selectShop.Name = txtShop.Text;
                if (m_ViewModel != null)
                {
                    m_ViewModel.Update(selectShop);
                }
            }
        }

        private void btnDeleteShop_Click(object sender, RoutedEventArgs e)
        {
            area selectShop = listShops.SelectedItem as area;
            if (selectShop != null)
            {
                if (m_ViewModel != null)
                {
                    m_ViewModel.DeleteShop(selectShop);
                }
            }
            else
            {
                MessageBox.Show("请选择店铺！");
            }
        }

        private void listShops_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (m_IsConfigUser)
            {
                area selectShop = listShops.SelectedItem as area;
                if (selectShop != null)
                {
                    txtShop.Text = selectShop.Name;
                    this.btnDeleteShop.IsEnabled = true;
                    this.btnUpdateShop.IsEnabled = true;
                }
                else
                {
                    this.btnDeleteShop.IsEnabled = false;
                    this.btnUpdateShop.IsEnabled = false;
                }
            }
        }

        private void txtShop_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtShop.Text))
            {
                this.btnAddShop.IsEnabled = false;
                this.btnUpdateShop.IsEnabled = false;
            }
            else
            {
                this.btnAddShop.IsEnabled = true;

                area selectShop = listShops.SelectedItem as area;
                if (selectShop != null)
                {
                    this.btnUpdateShop.IsEnabled = true;
                }
                else
                {
                    this.btnUpdateShop.IsEnabled = false;
                }
            }
        }

        private void ValidUserRole()
        {
            user user = SessionManager.Session["UserInfo"] as user;
            if (user != null)
            {
                if (user.RoleID == 3)
                {
                    m_IsConfigUser = false;
                    this.btnAddShop.IsEnabled = false;
                    this.btnDeleteShop.IsEnabled = false;
                    this.btnUpdateShop.IsEnabled = false;
                    this.txtShop.IsEnabled = false;
                }
                else
                {
                    m_IsConfigUser = true;
                    this.txtShop.IsEnabled = true;
                    this.btnAddShop.IsEnabled = true;
                    this.btnDeleteShop.IsEnabled = true;
                    this.btnUpdateShop.IsEnabled = true;
                }
            }
        }

    }
}
